<template>
  <div class="container">
    <van-nav-bar title="用户登录" left-text="首页" @click-left="$router.push('/films/nowPlaying')" />
    <img class="avatar" src="http://www.mobiletrain.org/images_index/right-fixed-face.png" alt="" />
    <van-divider />
    <!-- 表单 
    { validator: validatorUsername, message: '账号为关键字', trigger: 'onChange' }
    -->
    <van-form @submit="onSubmit" :show-error="false">
      <van-field
        v-model="username"
        name="username"
        label="账号"
        placeholder="请填写账号信息"
        :rules="[
          { required: true, message: '请填写账号信息' },
          { validator: validatorUsername, message: '账号为关键字' }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="请填写密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button class="login-btn" round block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: 'admin',
      password: 'admin888'
    }
  },
  methods: {
    onSubmit(values) {
      console.log('submit', values)
    },
    validatorUsername(value) {
      if (value === 'admina') {
        return false
      }
      return true
    }
    // 把ajax写入了
    // validatorUsername(value) {
    //   return new Promise((resolve, reject) => {
    //     setTimeout(() => {
    //       // resolve(false)
    //       if (value === 'admina') {
    //         resolve(false)
    //       }
    //       resolve(true)
    //     }, 1000)
    //   })
    // }
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  text-align: center;
  .avatar {
    width: 100px;
    height: 100px;
    margin: 20px auto;
  }
  .login-btn {
    background: #ff5f16;
    border: none;
    border-radius: 5px;
  }
}
</style>
